<?php


namespace EchoPHP\Session;

class SessionManager {

    /**
     * 应用程序容器
     * @var EchoPHP\Foundation\Application
     */
    protected $app;

    /**
     * session驱动合集
     * @var array
     */
    protected $drivers = [];

    public function __construct($app) {
        $this->app = $app;
    }

    /**
     * 生成session驱动
     * @return mixed
     */
    protected function driver() {
        //目前只支持使用memcache进行session驱动存储
        $driverName = $this->getDefaultDriver();
        if (!isset($this->drivers[$driverName])) {
            $this->drivers[$driverName] = $this->createDriver($driverName);
        }

        return $this->drivers[$driverName];
    }

    /**
     * 获取默认session存储驱动
     * @return string
     */
    protected function getDefaultDriver() {
        return 'memcached';
    }

    /**
     * 创建session驱动
     * @param $driver
     * @return MemcacheStore
     */
    protected function createDriver($driver) {
        switch ($driver) {
            case 'memcached':
                return $this->createMemcachedDriver();
        }
        throw new \InvalidArgumentException('尚未支持{' . $driver . '}session驱动');
    }

    /**
     * 创建基于memcache的session类
     * @return MemcacheStore
     */
    protected function createMemcachedDriver() {
        return new MemcacheStore();
    }

    public function __call($method, $parameters) {
        return $this->driver()->{$method}(...$parameters);
    }
}